'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0203
    Inherits Snac

    Public Sub New()
        MyBase.New(&H2, &H3)
    End Sub

    Private _ProfileMaxLenght As New TlvProfileMaxLength
    Public ReadOnly Property TlvProfileMaxLength() As TlvProfileMaxLength
        Get
            Return _ProfileMaxLenght
        End Get
    End Property

    Private _MaxCapabilities As New TlvMaxCapabilities
    Public ReadOnly Property MaxCapabilities() As TlvMaxCapabilities
        Get
            Return _MaxCapabilities
        End Get
    End Property

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        Do While index < data.Count
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H1
                    _ProfileMaxLenght.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H2
                    _MaxCapabilities.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            index += desc.TotalSize
        Loop

        SetTotalSize(index)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Throw New NotImplementedException
    End Function
End Class

Public Class TlvProfileMaxLength
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H1)
    End Sub

    Private _ClientMaxProfileLength As Integer
    Public Property ClientMaxProfileLength() As Integer
        Get
            Return _ClientMaxProfileLength
        End Get
        Set(ByVal value As Integer)
            _ClientMaxProfileLength = value
        End Set
    End Property

    Public Overrides ReadOnly Property DataSize() As Integer
        Get
            If MyBase.DataSize = 0 Then
                Return 2
            Else
                Return MyBase.DataSize
            End If
        End Get
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        _ClientMaxProfileLength = ByteConverter.ToUInt16(data.GetRange(Tlv.SizeFixPart, 2))
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_ClientMaxProfileLength)))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function
End Class

Public Class TlvMaxCapabilities
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H2)
    End Sub

    Private _MaxCapabilities As Integer
    Public Property MaxCapabilities() As Integer
        Get
            Return _MaxCapabilities
        End Get
        Set(ByVal value As Integer)
            _MaxCapabilities = value
        End Set
    End Property

    Public Overrides ReadOnly Property DataSize() As Integer
        Get
            If MyBase.DataSize = 0 Then
                Return 2
            Else
                Return MyBase.DataSize
            End If
        End Get
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        _MaxCapabilities = ByteConverter.ToUInt16(data.GetRange(Tlv.SizeFixPart, 2))
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_MaxCapabilities)))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function
End Class